/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cestum.cave.ui.components
{
import flash.display.Shape;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;

import flash.geom.Matrix;

public class Gradient extends Shape{
	
	private var _type:String=GradientType.LINEAR;
	public function set type(t:String):void{
		switch(t){
			case GradientType.RADIAL:
				_type=GradientType.RADIAL;
				break;
			case GradientType.LINEAR:
			default:
				_type=GradientType.LINEAR;
				break;
		}
	}
	public function get type():String{return _type;}
	
	private var _spread_method:String=SpreadMethod.PAD;
	public function set spread_method(sm:String):void{
		switch(sm){
			case SpreadMethod.REFLECT:
				_spread_method=SpreadMethod.REFLECT;
				break;
			case SpreadMethod.REPEAT:
				_spread_method=SpreadMethod.REPEAT;
				break;
			case SpreadMethod.PAD:
			default:
				_spread_method=SpreadMethod.PAD;
				break;
		}
	}
	public function get spread_method():String{return _spread_method;}
	
	private var _colors:Array=new Array(0xFFFFFF,0x000000);
	public function set colors(c:Array):void{_colors=c;}
	public function get colors():Array{return _colors;}
	
	private var _alphas:Array=new Array(1,1);
	public function set alphas(a:Array):void{_alphas=a;}
	public function get alphas():Array{return _alphas;}
	
	private var _ratios:Array=new Array(0,255);
	public function set ratios(r:Array):void{_ratios=r;}
	public function get ratios():Array{return _ratios;}
	
	public function Gradient(colors:Array=null,alphas:Array=null,ratios:Array=null){
		if(colors!=null)this.colors=colors;
		if(alphas!=null)this.alphas=alphas;
		if(ratios!=null)this.ratios=ratios;
	}
	
	public function render(w:Number,h:Number):void{
		
		//IMPLEMENT ROTATION ETC WHEN NEED ARISES...
		var matrix:Matrix = new Matrix();
		var box_rotation:Number = Math.PI/2; // 90˚
		var tx:Number = 0;
		var ty:Number = 0;
		matrix.createGradientBox(w,h,box_rotation,tx,ty);
		
		//IMPLEMENT GETTER SETTERS IF EVER NECESSARY...
		var interp:String = InterpolationMethod.LINEAR_RGB;
		var focalPtRatio:Number = 0;
		
		graphics.beginGradientFill(_type, 
			_colors,
			_alphas,
			_ratios, 
			matrix, 
			_spread_method, 
			interp, 
			focalPtRatio);
		graphics.drawRect(0,0,w,h);
	}
	

}
}